import redis
import time
import urllib.request as ur

class ProxyPoll:
    def __init__(self):
        self.redis_conn = redis.StrictRedis(
            host='localhost',
            port='6379',
            password='',
            decode_responses=True
        )

    def set_proxy(self):
        '''
        持续从第三方获取代理IP并更新至redis
        :return:
        '''
        proxy_old = None
        while True:
            proxy_new = ur.urlopen('http://api.ip.data5u.com/dynamic/get.html?order=f65ebccd58d38538fe5adb74d246b1f4&sep=4').read().decode('utf-8').strip().split(' ')
            if proxy_new != proxy_old:
                proxy_old = proxy_new
                self.redis_conn.delete('proxy')
                self.redis_conn.sadd('proxy',*proxy_new)
                print('代理IP已更新为:',proxy_new)
                time.sleep(2)
            else:
                time.sleep(1)

    def get_proxy(self):
        '''
        从redis获取持续更新的代理IP
        :return:
        '''
        proxy_s = self.redis_conn.srandmember('proxy',1)
        if proxy_s:
            return proxy_s[0]
        else:
            time.sleep(0.1)
            return self.get_proxy()

if __name__ == '__main__':
    ProxyPoll().set_proxy()